package com.example.szh.db;

import android.content.Context;

public class DBManager {
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    public void init(Context context){
        this.context=context;
    }

    public DaoMaster getDaoMaster() throws Exception {
        if (context==null){
            throw new Exception("DBManager context is null");
        }
        if (daoMaster==null){
            DaoMaster.DevOpenHelper openHelper=new DaoMaster.DevOpenHelper(context, "paper");
            daoMaster=new DaoMaster(openHelper.getWritableDatabase());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession() throws Exception {
        if (daoSession==null){
            daoSession=getDaoMaster().newSession();
        }
        return daoSession;
    }

    private static DBManager dbManager;
    public static synchronized DBManager getInstance(){
        if (dbManager==null){
            dbManager=new DBManager();
        }
        return dbManager;
    }
}
